/**
 * By Jay Liu
 */
package jay.lab.web.servlets;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <pre>
 * 生成圆图片, PNG格式.
 * P.S.生成圆角效果技巧:
 * overflow:hidden
 * height:4
 * float:left    float:right
 * </pre>
 * http://localhost:8080/eHaSms/arc?c=0xc3d9ff&h=20&w=20
 * @author <a href="#" title="MSN/GTalk : lakemove@gmail.com">Jay</a>
 * @since 2007-12-29 上午11:11:09
 */
public class ArcImageServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		response.setContentType("image/png");
		
		writeResponse(request, response);
	}
	
	private final static Map<String, BufferedImage> cache = new HashMap<String, BufferedImage>();
	private void writeResponse(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
	{
		String sc = request.getParameter("c");//颜色
		String sh = request.getParameter("h");//高度
		String sw = request.getParameter("w");//宽度
		String bg = request.getParameter("bg");//可选, 背景颜色, 用来fix ie的"png透明色bug", 见http://www.howtocreate.co.uk/alpha.html
		
		String key = "c-" + sc + "h-" + sh + "w-" + sw + "bg-" + bg;
		BufferedImage buffer = cache.get(key);
		
		if(buffer != null)
		{
			ImageIO.write(buffer, "png", response.getOutputStream());//png支持透明背景色, jpg, gif不支持
			return;
		}
		
		sc = sc.startsWith("0x") ? sc : "0x" + sc;//颜色是0xFFFFFF形式
		bg = bg == null ? null : bg.startsWith("0x") ? bg : "0x" + bg;
		
		if(bg == null)
			buffer = getImage(sc, Integer.decode(sh).intValue(), Integer.decode(sw).intValue());
		else
			buffer = getImage(sc, bg, Integer.decode(sh).intValue(), Integer.decode(sw).intValue());
		
		if(cache.size() < 150)//限制cache的大小
			cache.put(key, buffer);
		
		ImageIO.write(buffer, "png", response.getOutputStream());
	}
	
	/**
	 * 画个圆, 图片高度/宽度等于圆的高度/宽度
	 * 使用Graphics
	 */
	public BufferedImage getImage2(String color, int h, int w)
	{
		BufferedImage bi = new BufferedImage(h, w, BufferedImage.TYPE_INT_RGB);
		Graphics g = bi.createGraphics();
		Color c = Color.decode(color);
		c = new Color(c.getRed(), c.getGreen(), c.getBlue(), 255);//不透明
		g.setColor(new Color(255, 255, 255, 0));
		g.fillRect(0, 0, w, h);
		g.setColor(c);
		g.fillArc(0, 0, w, h, 0, 360);
		g.dispose();
		return bi;
	}
	
	/**
	 * 画个圆, 图片高度/宽度等于圆的高度/宽度
	 * 使用Graphics2D
	 */
	public BufferedImage getImage(String color, int h, int w)
	{
		BufferedImage bi = new BufferedImage(h, w, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = bi.createGraphics();
		
		g.setColor(new Color(255, 255, 255, 0));
		g.fillRect(0, 0, w, h);//填充透明的背景色
		
		Color c = Color.decode(color);
		c = new Color(c.getRed(), c.getGreen(), c.getBlue(), 255);//不透明
		g.setColor(c);
		Ellipse2D e2d = new Ellipse2D.Float(0, 0, w, h);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//消除锯齿
		g.fill(e2d);
		g.dispose();
		return bi;
	}
	
	/**
	 * 同时绘制背景色.
	 */
	public BufferedImage getImage(String color, String bg, int h, int w)
	{
		BufferedImage bi = new BufferedImage(h, w, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = bi.createGraphics();
		
		g.setColor(Color.decode(bg));
		g.fillRect(0, 0, w, h);//绘制背景色
		
		Color c = Color.decode(color);
		c = new Color(c.getRed(), c.getGreen(), c.getBlue(), 255);//不透明
		g.setColor(c);
		Ellipse2D e2d = new Ellipse2D.Float(0, 0, w, h);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//消除锯齿
		g.fill(e2d);
		g.dispose();
		return bi;
	}
}
